import time
from selenium import webdriver
from selenium.webdriver.common.by import By


# 定义一个元素定位类
class GpsElement:
    def __init__(self):
        # 初始化对象属性,打开谷歌浏览器,进入百度搜索
        self.driver = webdriver.Chrome()
        self.driver.get('https://www.baidu.com')

    def quit(self, seconds=3):
        time.sleep(seconds)
        self.driver.quit()

    # 1.用ID定位方法(元素ID)
    def id_element(self):
        kw = self.driver.find_element(By.ID, 'kw')
        print(kw.get_attribute('outerHTML'))

    # 2.用CLASS_NAME定位方法
    def class_name(self):
        s_ipt = self.driver.find_element(By.CLASS_NAME, 's_ipt')
        print(s_ipt.get_attribute('outerHTML'))

    # 3.用LINK_TEXT方法定位(链接文本)
    def link_text(self):
        new = self.driver.find_element(By.LINK_TEXT, '新闻')
        print(new.get_attribute('outerHTML'))

    # 4.用PARTIAL_LINK_TEXT方法定位(部分链接文本)
    def partial_link(self):
        news = self.driver.find_element(By.PARTIAL_LINK_TEXT, '新')
        print(news.get_attribute('outerHTML'))

    # 5.用NAME方法定位(元素名字)
    def name(self):
        input1 = self.driver.find_element(By.NAME, 'ie')
        print(input1.get_attribute('outerHTML'))

    # 6.用TAG_NAME方法定位(标签名字)
    def tag_name(self):
        inputs = self.driver.find_elements(By.TAG_NAME, 'input')
        print(inputs)
        print(len(inputs))
        for element in inputs:
            print(element.get_attribute('outerHTML'))


# 调用
if __name__ == '__main__':
    case = GpsElement()
    case.id_element()
    case.class_name()
    case.link_text()
    case.partial_link()
    case.name()
    case.tag_name()
    case.quit()
